﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProjectEulerSolutions.Problems
{
    /*
     * The minimum number of cubes to cover every visible face on a cuboid measuring 3 x 2 x 1 is twenty-two.

If we then add a second layer to this solid it would require forty-six cubes to cover every visible face, the third layer would require seventy-eight cubes, and the fourth layer would require one-hundred and eighteen cubes to cover every visible face.

However, the first layer on a cuboid measuring 5 x 1 x 1 also requires twenty-two cubes; similarly the first layer on cuboids measuring 5 x 3 x 1, 7 x 2 x 1, and 11 x 1 x 1 all contain forty-six cubes.

We shall define C(n) to represent the number of cuboids that contain n cubes in one of its layers. So C(22) = 2, C(46) = 4, C(78) = 5, and C(118) = 8.

It turns out that 154 is the least value of n for which C(n) = 10.

Find the least value of n for which C(n) = 1000.

     * */
    class Problem126 : IProblem
    {
        public string Calculate()
        {
            int limit = 20000;
            int[] C = new int[limit + 1];

            for (int a = 1; N(1, a, 1, 1) <= limit; a++)
                for (int b = 1; N(1, a, b, 1) <= limit && b <= a; b++)
                    for (int c = 1; N(1, a, b, c) <= limit && c <= b; c++)
                        for (int l = 1; N(l, a, b, c) <= limit; l++)
                            C[N(l, a, b, c)]++;


            for (int i = 0; i < C.Length; i++)
                if (C[i] == 1000)
                    return i.ToString();

            return "0";
        }

        long N(int l, int a, int b, int c)
        {
            //2(ab+ac+bc) za svaki novi layer - originalne prema vanka
            //4(l-1)a + 4(l-1)b + 4(l-1)c = 4(l-1)(a+b+c) za stranice, prvi layer nijednu, drugi jednu treci dvije i tako, 4 puta svaka
            //8(l-1)(l-2)/2 = 4(l-1)(l-2) za kutove, prva i druga nijedan, treca ima jedan, cetrvrta tri, peta pet itd (l-1 povrh 2)
            //jednadžba je malo skracena ispod (grupirane su druga i treca stavka, a u prvoj je a grupirano
            return 4 * (l - 1) * (a + b + c + l - 2) + 2 * (a * (b + c) + b * c);
        }
    }
}
